import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 添加到购物车
 */
public class addCart extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      response.setContentType("text/html:charset=utf-8");

      //获取商品名称
        String goodName = request.getParameter("name");

        //创建session对象
        HttpSession session = request.getSession();

        //session持久化操作
        Cookie cookie = new Cookie("JSESSIONID",session.getId() );
        cookie.setMaxAge(60*10);
        response.addCookie(cookie);

        //获取之前保存在session的list清单
        List list = (ArrayList) session.getAttribute("list");

        //第一次访问不存在list集合，则创建一个
        if (list==null){
            list = new ArrayList();

        }
        //存放所获取到的商品
        list.add(goodName);

        //session集合放入session中
        session.setAttribute("list",list);
        response.getWriter().print("购物车的商品是"+list.size()+"件商品");


    }


    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        doGet(request, response);
    }

}